import { Component, OnInit, ViewChild } from '@angular/core';
import { STColumn, STComponent, STChange, STData } from '@delon/abc/st';
import { SFComponent, SFSchema, SFDateWidgetSchema, SFUISchema } from '@delon/form';
import { ModalHelper, _HttpClient } from '@delon/theme';
import { NzModalService } from 'ng-zorro-antd/modal';
import { ContractManagementService } from '../../services/contract-management.service';
import { Router } from '@angular/router';

@Component({
  selector: 'app-contract-management-policy',
  templateUrl: './policy.component.html'
})
export class ContractManagementPolicyComponent implements OnInit {
  @ViewChild('st', { static: true })
  st!: STComponent;
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  @ViewChild('auditModal', { static: false })
  auditModal!: any;
  schema: SFSchema = {};
  columns: STColumn[] = [];
  ui: SFUISchema = {};
  @ViewChild('promoterModal', { static: false })
  promoterModal!: any;
  _$expand = false;

  selectedRows: any[] = [];
  paramValue = '';
  isLoading: boolean = false;
  constructor(public service: ContractManagementService, private modal: NzModalService, private router: Router) {}

  /**
   * 查询参数
   */
  get reqParams() {
    const params: any = {
      ...(this.sf && this.sf?.value)
    };
    delete params.expand;
    return params;
  }

  ngOnInit(): void {
    this.initST();
    this.initSF();
  }

  openDetail(item?: any) {
      this.paramValue = item?.paramValue
      const modal = this.modal.create({
        nzTitle: '传入值',
        nzContent: this.promoterModal,
        nzOnOk: () => {
          return;
        }
      });
    
  }

  initST() {
    this.columns = [
      { title: '', type: 'checkbox', width: '50px', className: 'text-center' },
      {
        title: '序号',
        className: 'text-center',
        width: '60px',
        format: (item: STData, col: STColumn, idx: number) => {
          return ((this.st.pi -1) * this.st.ps) + idx + 1 + ''
        } 
      },
      {
        title: '订单ID',
        width: '100px',
        className: 'text-center',
        index: 'billId'
      },
      {
        title: '项目ID',
        width: '100px',
        className: 'text-center',
        index: 'enterpriseProjectId'
      },
      {
        title: '保险公司',
        width: '100px',
        className: 'text-center',
        index: 'insuranceCompany'
      },
      {
        title: '投保金额',
        width: '100px',
        className: 'text-center',
        index: 'insureAmount'
      },
      
      {
        title: '保单号',
        width: '100px',
        className: 'text-center',
        index: 'policyNo'
      },
      {
        title: '保单地址',
        width: '100px',
        className: 'text-center',
        index: 'policyUrl'
      },
      {
        title: '保费',
        width: '100px',
        className: 'text-center',
        index: 'premium'
      },
      {
        title: '处理消息',
        width: '100px',
        className: 'text-center',
        index: 'processMessage'
      },
      {
        title: '处理结果',
        width: '100px',
        className: 'text-center',
        index: 'processResult'
      },
      {
        title: '操作',
        width: '170px',
        className: 'text-center',
        buttons: [
          {
            text: '查看传入值',
            click: item => {
              this.openDetail(item)
            },
            acl: { ability: ['CONTRACT-POLICY-view'] },
          }
        ]
      }
    ];
  }

  initSF() {
    this.schema = {
      properties: {
        _$expand: { type: 'boolean', ui: { hidden: true } },
        
        billId: {
          type: 'string',
          title: '订单id'
        },
        enterpriseProjectId: {
          type: 'string',
          title: '项目id'
        },
        insuranceCompany: {
          type: 'string',
          title: '保险公司'
        },
        policyNo: {
          type: 'string',
          title: '保单号',
          ui: {
            visibleIf: {
              _$expand: (value: boolean) => value
            },
          }
        },
        processResult: {
          type: 'string',
          title: '处理结果',
          enum: [
            { label: '全部', value: '' },
            { label: '成功', value: 1 },
            { label: '失败', value: 2 }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              _$expand: (value: boolean) => value
            },
            allowClear: true
          }
        }
      },
      type: 'object'
    };
    this.ui = { '*': { spanLabelFixed: 110, grid: { span: 8, gutter: 4 } } };
  }

  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
    this._$expand = false;
    this.isLoading = true
  }

  /**
   * 伸缩查询条件
   */
  expandToggle(): void {
    this._$expand = !this._$expand;
    this.sf?.setValue('/_$expand', this._$expand);
  }

  get queryFieldCount(): number {
    return Object.keys(this.schema?.properties || {}).length;
  }
  stChange(e: STChange): void {
    switch (e.type) {
      case 'checkbox':
        this.selectedRows = e.checkbox!;
        break;
      case 'filter':
        this.st.load();
        break;
    }
  }
}
